/* 
const { onShareAppMessage, onShareTimeline } = useShare({
    title: '分享标题',
    path: '/pages/detail/detail'
})

// 动态更新配置
const { updateShareConfig } = useShare()
updateShareConfig({
    title: '新标题',
    imageUrl: 'https://example.com/image.jpg'
})
 */

import { reactive } from 'vue'

const defaultConfig = {
    title: '',
    path: '/pages/tab/index/index',
    imageUrl: '',
    query: ''
}

/**
 * 分享Hook
 * @param {Object} shareConfig 分享配置
 * @returns {Object} 包含分享方法的对象
 */
const useShare = (shareConfig = {}) => {
    const shareInfo = reactive({
        ...defaultConfig,
        ...shareConfig
    })

    // 获取当前页面路径
    const getCurrentPath = () => {
        const pages = getCurrentPages()
        const currentPage = pages[pages.length - 1]
        return `/${currentPage.route}`
    }

    // 发送给朋友
    onShareAppMessage((res) => {
        if (res.from === 'button') {
            console.log('按钮分享:', res)
        }
        return {
            ...shareInfo,
            path: shareInfo.path || getCurrentPath()
        }
    })

    // 分享到朋友圈 (仅微信小程序)
    // #ifdef MP-WEIXIN
    onShareTimeline((res) => {
        if (res.from === 'button') {
            console.log('朋友圈按钮分享:', res.target)
        }
        return {
            ...shareInfo,
            path: shareInfo.path || getCurrentPath()
        }
    })
    // #endif

    return { 
        onShareAppMessage, 
        onShareTimeline,
        updateShareConfig: (config) => {
            Object.assign(shareInfo, config)
        }
    }
}

export { useShare }